package mysql

import (
	"gorm.io/gorm"
	"video-hub/videorpc/videorpc"
)

type Collect struct {
	gorm.Model
	UserId        int    `gorm:"type:int(11);comment:用户Id"`
	VideoId       int    `gorm:"type:int(11);comment:视频Id"`
	CollectTime   string `gorm:"type:varchar(30);comment:收藏时间"`
	CollectStatus int    `gorm:"type:int(11);comment:收藏状态"`
}

func (c *Collect) CollectAdd(db *gorm.DB) error {
	return db.Debug().Create(c).Error
}

func (c *Collect) FindUserVideo(db *gorm.DB, in *videorpc.CollectAddRequest) error {
	return db.Debug().Where("user_id=? ", in.UserId).Where("video_id=?", in.VideoId).Find(c).Error
}

func (c *Collect) FindUserId(db *gorm.DB, id int64) error {
	return db.Debug().Where("collects.user_id=?", id).First(c).Error
}

func (c *Collect) CollectDel(db *gorm.DB, id int64) error {
	return db.Debug().Where("id=?", id).Delete(c).Error
}

func (c *Collect) FindCollectId(db *gorm.DB, id int64) error {
	return db.Debug().Where("id=?", id).Find(c).Error
}
func CollectList(db *gorm.DB, in *videorpc.CollectListRequest) ([]*videorpc.CollectList, error) {
	var list []*videorpc.CollectList
	//SELECT
	//  users.url,
	//  video_title,
	//  nick_name,
	//  num
	//FROM
	//  collects
	//  LEFT JOIN users ON collects.user_id
	//  LEFT JOIN videos ON collects.video_id
	db.Debug().Model(&Collect{}).Select(
		"users.url",
		"video_title",
		"nick_name",
		"num",
	).Joins("LEFT JOIN users ON collects.user_id").
		Joins("LEFT JOIN videos ON collects.video_id").
		Where("collects.user_id=?", in.UserId).
		Scopes(Paginate(int(in.Page), int(in.Size))).
		Find(&list)
	return list, err
}
